package com.tuju.currency.base

import android.app.Activity
import android.app.Application
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import com.tuju.currency.utils.CommonUtils
import com.tuju.currency.utils.statubar.StatusBarUtil
import rx.Subscription
import rx.subscriptions.CompositeSubscription

open class BaseActivity : AppCompatActivity() {

    protected var mCompositeSubscription: CompositeSubscription? = null

//    override fun setContentView(layoutResID: Int) {
//        super.setContentView(layoutResID)
////        setCustomDensity(this, application)
//    }

    fun addSubscription(s: Subscription?) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = CompositeSubscription()
        }
        mCompositeSubscription?.add(s)
    }

    fun removeSubscription() {
        mCompositeSubscription?.unsubscribe()
    }

    override fun onDestroy() {
        super.onDestroy()
        mCompositeSubscription?.unsubscribe()
    }

    //设置状态栏颜色
    open fun setStateBarColor(color: Int) {
        StatusBarUtil.setColor(this, CommonUtils.getColor(color))
    }

    //今日头条适配方案
    fun setCustomDensity(@NonNull activity: Activity, @NonNull application: Application) {
        val appDisplayMetrics = application.resources.displayMetrics
        val targetDensity = (appDisplayMetrics.widthPixels / 360).toFloat()
        val targetDensityDpi = (160 * targetDensity).toInt()

        appDisplayMetrics.scaledDensity = targetDensity
        appDisplayMetrics.density = appDisplayMetrics.scaledDensity
        appDisplayMetrics.densityDpi = targetDensityDpi

        val activityDisplayMertrics = activity.resources.displayMetrics
        activityDisplayMertrics.scaledDensity = targetDensity
        activityDisplayMertrics.density = activityDisplayMertrics.scaledDensity
        activityDisplayMertrics.densityDpi = targetDensityDpi
    }

}